﻿"use strict";

function Combat(egos)
{
    this.egos = egos;
    this.lump = [];
}
//do not use till cleaned up, supposed to group the combatants into a battle screen so that the battle calcs can be done without affecting others.
Combat.prototype.Execute = function (args)
{
    var obj = JSON.parse(args);
    alert(obj);
}
Combat.prototype.Run = function (args)
{
    // Start combat

    this.combatants = [];
    d = {};
    for (var i = 0; i < arguments.length; i++)
    {
        var ego = this.egos[arguments[i]];
        d[ego.name] = ego;
        for (var j = 0; j < ego.group.length; j++)
        {
            ego2 = this.egos[ego.group[j]];

            if (ego2 == null)
            {
                log('Unknown player ' + name);
            }
            else
            {
                d[ego2.name] = ego2;
            }
        }
    }

    for (var key in d)
    {
        this.combatants.push(d[key]);
    }

    var s = '';
    s += 'Starting combat with the following participants:\n';
    for (var i = 0; i < this.combatants.length; i++)
    {
        s += '    ' + this.combatants[i].name + '\n';
    }
    log(s);
}
//forces the players and monsters to go after eachother, dumb routine. Needs to get input from the players on which attacks they are going to do
//and cycle through a randomizer for the enemys, unless it is a specific fight, ie a boss fight
Combat.prototype.Attack = function (attack)
{
    var ego = this.egos;
    ego.player1.Attack(ego.monster1, attack.index);
    var max = 0;
    for (var i in attacks)
    {
        max++
    }
    var index = Math.floor(Math.random() * (max));
    ego.monster1.Attack(ego.player1, index);
    //for (var i = 0; i < this.combatants.length; i++)
    //{
    //    this.combatants[i].attack = attacks[attack];
    //}
    //ego.player1.Attack(ego.monster1);
    //ego.monster1.Attack(ego.player1);
    //ego.player1.TickEffects(ego.monster1);
    //ego.monster1.TickEffects(ego.player1);
    try
    {
        console.dir(ego.monster1);
        console.dir(ego.player1);
    }
    catch (ex) { }
}

Combat.prototype.lump = {};
Combat.prototype.ChooseAttack = function ()
{
    var attacks = {
        strength: ['Basic Attack', 'Sword'],
        magic: ['Fireball'],
        agility: ['Multi-Shot'],
        willpower: ['Smite'],
        items: ['Health Flower', 'Bomb'],
        skip: []
    };

    var selectAttack = document.getElementById('selectAttack');
    var selectAction = document.getElementById('selectAction');
    var txt = document.getElementById('txtOutput');

    var choice = selectAttack.value;
    var lst = attacks[choice];
    selectAction.options.length = 0;

    for (var i = 0; i < lst.length; i++)
    {
        var option = document.createElement("option");
        option.text = lst[i];
        try
        {
            selectAction.add(option, null);
        } catch (error)
        {
            selectAction.add(option);
        }
    }
}

Combat.prototype.makeSelection = function ()
{
    var s = '';
    var txt = document.getElementById('txtCombat');

    this.lump['Player 1'] = ['Player 1', 'Monster 1', 'magic', 'Fireball'];
    this.lump['Player 2'] = ['Player 2', 'Monster 1', 'magic', 'Fireball'];
    this.lump['Player 3'] = ['Player 3', 'Monster 2', 'magic', 'Fireball'];
    this.lump['Player 4'] = ['Player 4', 'Monster 2', 'magic', 'Fireball'];

    for (key in this.lump)
    {
        s = s + ' ' + this.lump[key][0] + ' ' + this.lump[key][1] + ' ' + this.lump[key][2] + ' '+ this.lump[key][3] + ' ';
    }
    txt.innerHTML = s;
    txt.scrollTop = txt.scrollHeight;

   // alert('sss');
}


Combat.prototype.AddToList = function ()
{
    var s = '';
    var txt = document.getElementById('txtCombat');
    var player = document.getElementById('selectPlayer').value;
    var target = document.getElementById('selectTarget').value;
    var attack = document.getElementById('selectAttack').value;
    var action = document.getElementById('selectAction').value;
    this.lump[player] = [player, target, attack, action];
    var txt = document.getElementById('txtCombat');
    for (key in this.lump)
    {
        s = s + ' ' + this.lump[key][0] + ' ' + this.lump[key][1] + ' ' + this.lump[key][2] + ' ' + this.lump[key][3] + ' ';
    }
    txt.innerHTML = s;
    txt.scrollTop = txt.scrollHeight;
}

Combat.prototype.LookUpByName = function (playername)
{
    for (var i = 0; i < this.egos.length; i++)
    {
        if (this.egos[i].name == playername)
        {
            return (this.egos[i]);
        }
    }
    return null;
}

Combat.prototype.Run = function ()
{
    var s = '';
    var isfound = false;
    var player;
    var monster;
    var playernum = [this.LookUpByName('Player 1'), this.LookUpByName('Player 2'), this.LookUpByName('Player 3'), this.LookUpByName('Player 4')];


    for (playername in this.lump)
    {
        var playerchoice = this.lump[playername];
        player = this.LookUpByName(playername);

        if (player == null) { alert(playername + ' is null'); return; }

        var target = this.LookUpByName(playerchoice[1]);
        var attack = playerchoice[3];
        log(playername + ' ' + playerchoice[1] + ' ' + attack + '. ');
        player.Attack(target, attack);
        //playernum.push(player);
    }

    //Math.floor(Math.random() * (max - min + 1)) + min

    var monsters = ['Monster 1', 'Monster 2', 'Monster 3', 'Monster 4']

    for (var i = 0; i < monsters.length; i++)
    {
        monster = this.LookUpByName(monsters[i]);
        if (monster == null)
        {
            alert(monster + ' is null');
            return;
        }
        var attackindex = Math.floor(Math.random() * (monster.attacks.length - 1));
        var attack = monster.attacks[attackindex];
        var playerindex = Math.floor(Math.random() * (playernum.length - 1));
        var target = playernum[playerindex];

        monster.Attack(target, attack);

    }


    for (key in this.lump)
    {
        s = s + '' + this.lump[key][0] + this.lump[key][1] + this.lump[key][2] + this.lump[key][3] + '';
    }

    //alert(this.lump);
}



Combat.prototype.ClearList = function ()
{
    document.getElementById('txtCombat').innerHTML = '';
    this.lump = [];
}



